var app = angular.module('webApp', ['ui.router', 'oc.lazyLoad', 'ngCookies','iService','iDirective','Filter','angularFileUpload','akoenig.deckgrid','ngStorage']);
// 页面路由
app.config(['$controllerProvider','$stateProvider','$urlRouterProvider', '$locationProvider','$ocLazyLoadProvider',function($controllerProvider,$stateProvider,$urlRouterProvider,$locationProvider,$ocLazyLoadProvider) {
    //动态拆分控制器，指令，过滤器，工厂，服务
    app.register = {
        controller: $controllerProvider.register,
        /*directive: $compileProvider.directive,
        filter: $filterProvider.register,
        factory: $provide.factory,
        service: $provide.service,
        constant: $provide.constant*/
    };
    //路由状态重写
    var loadResolve = function (files,options) {
        var loadPlugin = {
            deps: ['$ocLazyLoad', function($ocLazyLoad) {
                return $ocLazyLoad.load({
                    files: files
                });
            }]
        }
        if(options){
            angular.forEach(options,function (value,key) {
                loadPlugin[key] = value;
            })
        }
        return loadPlugin;
    }
    //动态路由
    $stateProvider
    .state('home', {    //首页
        url:'/home',
        templateUrl: __uri('/views/home.html'),
        controller:'homeCtrl',
        resolve : loadResolve([__uri('../css/home.less')])
    }).state('descrip', {    //活动说明
        url: '/descrip',
        templateUrl: __uri('/views/descrip.html'),
        controller:'otherCtrl',
        resolve : loadResolve([__uri('../css/descrip.less')])
    })/*.state('sign', {    //报名
        url:'/sign',
        templateUrl: 'views/sign-up.html',
        controller:'signCtrl',
        resolve : loadResolve([__uri('../css/sign.less')],{
            // I will cause a 1 second delay
            delay: ['$q', 'Context',function($q, Context) {
                var delay = $q.defer();
                if(!Context.isLogin()){
                    location.href = 'http://h5.oeeee.com/index.php?s=/Zmhhr';
                }else{
                    delay.resolve();
                }
                return delay.promise;
            }]
        })
    })*/.state('list', {    //活动列表
        url: '/list',
        templateUrl: __uri('/views/listlist.html'),
        controller:'listlistCtrl',
        resolve : loadResolve([__uri('../css/listlist.less')])
    }).state('detail', {    //个人详情
        url: '/detail/:uid',
        templateUrl: __uri('/views/detaildetail.html'),
        controller:'detailCtrl',
        resolve : loadResolve([__uri('../css/detail.less'),__uri('./ctrl/detail.js')])
    }).state('poster', {    //个人海报
        url: '/poster/:type/:src',
        templateUrl: __uri('/views/poster.html'),
        controller:'posterCtrl'
    }).state('rematch', {    //复赛
        url: '/rematch',
        templateUrl: __uri('/views/rematch.html'),
        controller:'rematchCtrl',
        resolve : loadResolve([__uri('../css/listlist.less'),__uri('../css/rematch.less')])
    }).state('redetail', {    //复赛
        url: '/redetail/:uid',
        templateUrl: __uri('/views/redetail.html'),
        controller:'redetailCtrl',
        resolve : loadResolve([__uri('../css/detail.less'),__uri('./ctrl/detail.js')])
    });
    // .state('list', {    //活动列表
    //     url: '/list',
    //     templateUrl: 'views/list.html',
    //     controller:'listCtrl',
    //     resolve : loadResolve([__uri('../css/list.less')])
    // }).state('detail', {    //个人详情
    //     url: '/detail/:uid',
    //     templateUrl: 'views/detail.html',
    //     controller:'detailCtrl',
    //     resolve : loadResolve([__uri('../css/detail.less'),__uri('./ctrl/detail.js')])
    // })
    $urlRouterProvider.otherwise('/home');
    // $locationProvider.html5Mode(true);
    $locationProvider.hashPrefix('');


}]);

// 在所有route跳转前进行登录检查，目标页面需要登录且用户未登录，则跳转到登录页面
app.run(['$rootScope', '$cookies', '$location', function($rootScope,$cookies,$location) {
    $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
        // $cookies.wx_h5_wxuid= 'chen'
        window.scrollTo(0,0);
        var localUrl = location.href;
        if(localUrl.indexOf('?from=singlemessage') >= 0){
            localUrl = localUrl.replace('?from=singlemessage','');
            // console.log(localUrl);
            location.replace(localUrl);
        }
        $location.search({});
        // 加载wx的key
        if(toState.name != 'detail' && toState.name != 'detaildetail' && toState.name != 'poster' && toState.name != 'redetail'){
            $rootScope.wxshare(false);
        }
    })
}]);

app.controller('appCtrl',['$scope','$rootScope','$location','$timeout','$cookies', function ($scope,$rootScope,$location,$timeout,$cookies) {
    //设置title
    $scope.update_title = function(title) {
        var body = document.getElementsByTagName('body')[0];
        document.title = title;
        var iframe = document.createElement("iframe");
        iframe.setAttribute("src", "/empty.png");

        iframe.addEventListener('load', function() {
            setTimeout(function() {
                iframe.removeEventListener('load');
                document.body.removeChild(iframe);
            }, 0);
        });
        document.body.appendChild(iframe);
    }

    //分享设置
    $rootScope.wxshare = $scope.wxshare = function(boolean,type){
        if($config.iswx()){
            if(boolean){
                var shareConfig = {};
                if(type == 1){
                    shareConfig.title = '我是'+($cookies.hhrname?$cookies.hhrname:'合伙人')+'，我在竞选“寻找最美合伙人”创客大赛，快投票支持我吧！';
                }else{
                    shareConfig.title = '我是'+($cookies.hhrname?$cookies.hhrname:'合伙人')+'，我在竞选《校花合伙人》电影女主角，快投票支持我吧！';
                }
                shareConfig.url = location.href;
                $config.wxShare(shareConfig);
            }else{
                $config.wxShare();
            }
        }
    }
}]);
